package sy.services.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.db.mysql.dao.TresourceDao;
import sy.db.mysql.dao.TuserDao;
import sy.db.mysql.dao.TuserTroleDao;
import sy.db.mysql.model.Tuser;
import sy.db.mysql.model.TuserTrole;
import sy.pageModel.DataGrid;
import sy.pageModel.PageHelper;
import sy.pageModel.SessionInfo;
import sy.pageModel.Tree;
import sy.services.UserServiceI;
import sy.util.MD5Util;

@Service
public class UserServiceImpl implements UserServiceI {
	
	@Autowired
	TuserDao tuserDao;
	@Autowired
	TuserTroleDao tuserTroleDao;
	@Autowired
	TresourceDao tresourceDao;
	@Override
	public Map login(Map map) {
		map.put("PWDSTR_EQ", MD5Util.md5((String)map.get("PWDSTR_EQ")));
		List<Map> users = tuserDao.findbyByGrid(map);
		if(users!=null && users.size()>0){
			return users.get(0);
		}
		return null;
	}

	@Override
	public void reg(Tuser user) throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("NAMESTR_EQ", user.getNAMESTR());
		List<Map> users = this.tuserDao.findbyByGrid(params);
		if (users!=null && users.size()>0) {
			throw new Exception("登录名已存在！");
		} else {
			Tuser u = new Tuser();
			u.setID(UUID.randomUUID().toString());
			u.setNAMESTR(user.getNAMESTR());
			u.setPWDSTR(MD5Util.md5(user.getPWDSTR()));
			u.setCREATEDATE(new Date());
			this.tuserDao.insertSelective(u);
		}
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public DataGrid dataGrid(Map map) {
		DataGrid dataGrid = new DataGrid();
		map.put("dataGrid", dataGrid);
		List<Map> retList = this.tuserDao.findbyByGrid(map);
		for(Map user : retList) {
			try {
				List<Map> reslist = this.tuserDao.findResStrByUserID(user.get("ID").toString());
				if(reslist!=null && reslist.size()>0 && reslist.get(0).get("ROLEIDS")!=null)
				user.putAll(reslist.get(0));
			} catch (Exception e) {
			}
		}
		dataGrid.setRows(retList);
		return dataGrid;
	}
	
	@Override
	public void add(Tuser user) throws Exception {
		user.setPWDSTR(MD5Util.md5(user.getPWDSTR()));
		this.tuserDao.insertSelective(user);
	}

	@Override
	public Tuser get(String id) {
		return	this.tuserDao.selectByPrimaryKey(id);
	}

	@Override
	public void edit(Tuser user) throws Exception {
		this.tuserDao.updateByPrimaryKeySelective(user);
		
	}

	@Override
	public void delete(String id) {
		this.tuserDao.deleteByPrimaryKey(id);
	}

	@Override
	public void grant(String ids, Tuser user) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("TUSERIDSTR_EQ", user.getID());
		List<TuserTrole> userRleos = this.tuserTroleDao.findbyByGrid(params);
		for(TuserTrole tr :userRleos ){
			this.tuserTroleDao.deleteByPrimaryKey(tr.getID());
		}
		for (String id : ids.split(",")) {
			TuserTrole tr = new TuserTrole();
			tr.setTUSERIDSTR(user.getID());
			tr.setTROLEIDSTR(id);
			tr.setID(UUID.randomUUID().toString());
			this.tuserTroleDao.insertSelective(tr);
		}
	}

	@Override
	public List<String> resourceList(String id) {
		return this.tresourceDao.selectByUserID(id);
	}

	@Override
	public void editPwd(Tuser user) {
		user.setPWDSTR(MD5Util.md5(user.getPWDSTR()));
		this.tuserDao.updateByPrimaryKeySelective(user);
	}

	@Override
	public boolean editCurrentUserPwd(SessionInfo sessionInfo, String oldPwd,
			String pwd) {
		Tuser u = tuserDao.selectByPrimaryKey(sessionInfo.getId());
		if (u.getPWDSTR().equalsIgnoreCase(MD5Util.md5(oldPwd))) {// 说明原密码输入正确
			u.setPWDSTR(MD5Util.md5(pwd));
			u.setMODIFYDATE(new Date());
			tuserDao.updateByPrimaryKeySelective(u);
			return true;
		}
		return false;
	}

	@Override
	public List<Tuser> loginCombobox(String q) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DataGrid loginCombogrid(String q, PageHelper ph) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Long> userCreateDatetimeChart() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 地区查询
	 */
	@Override
	public List<Tree> tree() {
		return this.tuserDao.findDqbm();
	}
	
}
